package com.jt.tenant.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.jt.tenant.R;
import com.jt.tenant.adapter.FaultAdapter;
import com.jt.tenant.conn.ClearFault;
import com.jt.tenant.conn.GetFaultList;
import com.jt.tenant.widget.UpdateService;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilApp;
import com.zcx.helper.util.UtilToast;

import java.util.ArrayList;
import java.util.List;

//import com.jt.tenant.conn.GetSoftwareUpdate;

/**
 * Created by Administrator on 2016/2/14.
 */
public class ErrorCodeActivity extends JTActivity implements View.OnClickListener {
    private TextView title_name;

    private TextView start_clear;

    private List<GetFaultList.Info> list1 = new ArrayList<>();

    private ListView listView;

    private FaultAdapter adapter;

    private LinearLayout mLineYes, mLineNo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_errorcode);

        initView();

        GetList();

    }

    private void initView() {

        (title_name = (TextView) findViewById(R.id.tv_title_name)).setText("故障码");

        listView = (ListView) findViewById(R.id.fault_list_view);

        mLineYes = (LinearLayout) findViewById(R.id.lin_errorcode_yes);

        mLineNo = (LinearLayout) findViewById(R.id.lin_errorcode_no);

        (start_clear = (TextView) findViewById(R.id.btn_errorcode_clear)).setOnClickListener(this);

        adapter = new FaultAdapter(this, list1);

        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Intent Intent = new Intent(ErrorCodeActivity.this,FaultAnalysisActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("descrption",list1.get(position).descrption);
                bundle.putString("suggest",list1.get(position).suggest);
                Intent.putExtras(bundle);
                startActivity(Intent);

            }
        });

    }

    private void GetList() {

        new GetFaultList(new AsyCallBack<List<GetFaultList.Info>>() {

            @Override
            public void onSuccess(String toast, int type, List<GetFaultList.Info> list) throws Exception {


                list1.addAll(list);

                if (toast.equals("000")) {

                    mLineNo.setVisibility(View.VISIBLE);

                    adapter.notifyDataSetChanged();

                } else {

                    mLineYes.setVisibility(View.VISIBLE);

                    Toast.makeText(ErrorCodeActivity.this, toast, Toast.LENGTH_SHORT).show();

                }

            }
        }).execute(ErrorCodeActivity.this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.btn_errorcode_clear:

                StringBuilder str = new StringBuilder();

                for (int i = 0; i < list1.size(); i++) {

                    if (i == list1.size() - 1) {

                        str.append(list1.get(i).id);

                    } else {

                        str.append(list1.get(i).id + ",");

                    }
                }

                Log.e("TAG", "str=" + str);

                if(str.equals("")){

                    Toast.makeText(this, "无可清除故障码", Toast.LENGTH_SHORT).show();

                }else {

                    new ClearFault(new AsyCallBack<ClearFault.Info>() {

                        @Override
                        public void onSuccess(String toast, int type, ClearFault.Info info) throws Exception {

                            if (info.msg.equals("000")) {

                                UtilToast.show(context,"清除成功！");

                                list1.clear();

                                adapter.notifyDataSetChanged();

                            } else {

                                UtilToast.show(context,info.msg);

                            }

                        }
                    }).execute(context);

                }

                break;

        }

    }


}
